// eslint-disable-next-line @typescript-eslint/no-var-requires
const tencentcloud = require("tencentcloud-sdk-nodejs");

const AsrClient = tencentcloud.asr.v20190614.Client;

const clientConfig = {
  credential: {
    secretId: "AKIDCG7k8IEyUlofHiura7QWO2SdaKp56uTn",
    secretKey: "81e3C5j7GDvPfhV5QWNzNXS8annY4MPE",
  },
  region: "",
  profile: {
    httpProfile: {
      endpoint: "asr.tencentcloudapi.com",
    },
  },
};

const client = new AsrClient(clientConfig);

// 通过录音文件获取id
export function getTaskIdByUrlService(url: string) {
  return new Promise<IAnyObject>((resolve, reject) => {
    client.CreateRecTask({
      EngineModelType: '16k_zh',
      ChannelNum: 1,
      ResTextFormat: 0,
      SourceType: 0,
      Url: url,
    })
      .then((res: IAnyObject) => resolve(res))
      .catch((err: IAnyObject) => reject(err))
  })
}

// 通过id获取语音信息
export function getYuyinByIdService(id: number) {
  return new Promise((resolve, reject) => {
    const timer = setInterval(() => {
      client.DescribeTaskStatus({
        TaskId: id,
      })
        .then((res: IAnyObject) => {
          if (res.Data.StatusStr == 'success') {
            clearInterval(timer)
            resolve(res.Data)
          } else if (res.Data.StatusStr == 'failed') {
            clearInterval(timer)
            reject('失败');
          }
        })
        .catch((err: any) => reject(err));
    }, 50)
  })
}